<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-11-14
 * Time: 8:46
 */

namespace app\store\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Peifang_old extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'StoreOrderGoods';

    /**
     * 配方列表
     * @return array
     */
    public function index()
    {
        $this->title = '待订货商品配方列表';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods g', 'a.goods_id=g.id', 'LEFT'],
            ['ys_goods_category c', 'g.cat_id=c.id', 'LEFT'],
            ['ys_goods_spec_detail s', 'a.goods_spec_id=s.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->where('a.store_id', $store_id)->where('a.is_deleted', 0)->where('a.status', 0)->where('is_diannei',0)->field('g.name,c.title as cname,a.id,a.addtime,g.unit,a.num,s.title')->join($join);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        $qiniu = config('qiniu');
        $this->assign('qiniu', $qiniu['yangshi']);          //七牛样式后缀

        return parent::_list($db);

    }

    /**
     * 商品配方添加
     * @return array|string
     */
    public function plus()
    {
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {
            if (!input('goods_id'))
                $this->error('请选择一种商品');
            if (!input('goods_spec_id') && input('goods_id') && db('goods')->find(input('goods_id'))['is_spec'] == 1)
                $this->error('请选择一种属性');

            if (input('goods_spec_id'))
                $spec_id = input('goods_spec_id');
            else
                $spec_id = db('goods_spec_detail')->where('goods_id',input('goods_id'))->where('specs',0)->find()['id'];

            //订货配方
            $order_goods_id = db($this->table)->insertGetId(['goods_id' => input('goods_id'), 'goods_spec_id' => $spec_id, 'num' => input('num'), 'store_id' => $store_id, 'addtime' => time()]);

            if ($order_goods_id)
                $this->success('配方添加成功！', '');
        } else {
            $cat = db('goods_category')->where('is_exp',0)->select();
            $this->assign('cat', $cat);

            return $this->_form($this->table, 'plus');
        }
    }

    public function goods()
    {
        $goods = db('goods')->where('cat_id', input('cat_id'))->where('store_id','0')->where('is_deleted',0)->where('status',1)->select();
        return $goods;
    }

    public function spec()
    {
        $spec = db('goods_spec_detail')->where(['goods_id'=>input('goods_id'),'is_deleted'=>0])->select();
        return $spec;
    }

    /**
     * 编辑
     * @return array|string
     */
    public function edit()
    {
        $cat = db('goods_category')->select();
        $this->assign('cat', $cat);

        return $this->_form($this->table, 'plus');
    }


    /**
     * 删除
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("配方删除成功！", '');
        }
        $this->error("配方删除失败，请稍候再试！");
    }

    /**
     * 生成订货单
     * @throws \think\Exception
     */
    public function order()
    {
        if (DataService::update($this->table)) {
            $sn = build_order_no();
            $store_id = session('user')['store_id'];
            $i = db('store_order')->insertGetId(['sn' => $sn, 'addtime' => time(), 'store_id' => $store_id]);

            $ids = input('id');
            $ids = explode(',', $ids);
            //遍历商品配方
            $status = 1;//循环报错为0
            foreach ($ids as $id) {
                $ret = db($this->table)->where('id', $id)->update(['order_id' => $i]);
                if ($ret === false)
                    $status = 0;
            }
            if ($status == 1)
                $this->success("订货单生成成功！", '');
        }
        $this->error("订货单生成失败，请稍候再试！");
    }

}